﻿using System.Collections;
using System.Net.Security;
using System.Runtime.CompilerServices;

namespace LxBasic
{
    /// <summary>
    /// 编号的范围，具体为[start,end)
    /// </summary>
    public struct IndexRange : IEnumerable<int>
    {
        public IndexRange()
        {
            Start = 0;
            End = 0;
        }
        public IndexRange(int start, int end)
        {
            Start = start;
            End = end;
        }
        public IndexRange(int count)
        {
            Start = 0;
            End = count;
        }
        public int Start;
        public int End;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public (int start, int end) Split(ThreadInfo threadId)
        {
            int nodeCount = GetCount();
            return (Start + nodeCount * threadId.CurrentThreadID / threadId.TotalThreadsCount, Start + nodeCount * (threadId.CurrentThreadID + 1) / threadId.TotalThreadsCount);
        }
        public int GetCount() => End - Start;
        public bool Contains(int id) => id >= Start && id < End;
        public bool IsEmpty() => GetCount() == 0;
        public (int Start, int End) Get()
        {
            return (Start, End);
        }
        public override string ToString()
        {
            return string.Format($"[{Start},{End})");
        }
        public IEnumerator<int> GetEnumerator()
        {
            int i1 = Start;
            while (i1 < End)
            {
                yield return i1;
            }
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
           return GetEnumerator();
        }
    }
}
